import { AdManager } from "../ads/AdManager";
import { BUNDLE_TYPE_ENUM, RELEASE_TYPE_ENUM } from "../base/EnumIndex";
import Singleton from "../base/Singleton";
import { Tools } from "../common/Tools";
import { LevelManager } from "./LevelManager";
import { UIManager } from "./UIManager";

export class GameManager extends Singleton {
    static get ins() {
        return super.getInstance<GameManager>();
    }

    startUITimes: number = 0;

    startUIPop() {
        // // 首次打开开始页面不弹出
        // if (this.startUITimes == 0) {
        //     this.startUITimes++;
        //     return;
        // }
        this.startUITimes++;
        // 解锁全部关卡 和 添加桌面 交替弹出
        if (this.startUITimes % 2 == 0) {
            // AdManager.isShowAddShortcutBtn((active: boolean) => {
            //     if (active) UIManager.ins.showPop('addShortcutUI', BUNDLE_TYPE_ENUM.HOME);
            // });
        } else {
            if (!LevelManager.ins.isUnlockAll()) {
                UIManager.ins.showPop("LockLvUI", BUNDLE_TYPE_ENUM.HOME, true);
            }
        }
    }

    passLevel() {
        let idx = LevelManager.ins.getLvInfoIdx(LevelManager.ins.curLvInfo.type, LevelManager.ins.curLvInfo.lv) + 1;



        AdManager.reportVideo(5, idx, LevelManager.ins.curLvInfo.name);
        AdManager.naoLiReportVideo(2, "跳过本关");
        UIManager.ins.enabledClick(false);
        AdManager.showVideoAd(() => {
            UIManager.ins.enabledClick(true);
            AdManager.reportOverVideo(idx, LevelManager.ins.curLvInfo.name);
            AdManager.exitLv(LevelManager.ins.curLvInfo.name);
            //临时解锁下一关
            let nextLvInfo = LevelManager.ins.getNextLvInfo();
            if (LevelManager.ins.isLiveing) {
                nextLvInfo = LevelManager.ins.getNextLiveLvInfo();
            }
            if (nextLvInfo) {
                LevelManager.ins.curLvInfo = nextLvInfo;
                LevelManager.ins.unlockCurrentLevelTemp();
                // LevelManager.ins.unlockNextLevel();
                UIManager.ins.closeAll();
                LevelManager.ins.loadLevelPre();
            } else {
                UIManager.ins.closeAll();
                UIManager.ins.showUI("LevelUI", BUNDLE_TYPE_ENUM.HOME);
            }
            AdManager.naoLiReportVideo(3, "跳过本关");
            AdManager.naoLiReportVideo(4, "跳过本关");
        }, () => {
            UIManager.ins.enabledClick(true);
        });
    }

    canShowRecommendUI() {
        let time = Tools.getStorage("recommendTime");
        if (!time) return true;
        let timeNow = new Date();
        let timeLast = new Date(time);
        cc.log(timeNow.getMonth(), timeLast.getMonth(), timeNow.getDay(), timeLast.getDay())
        if (timeNow.getMonth() === timeLast.getMonth() && timeNow.getDay() === timeLast.getDay()) {
            /** 同一天 */
            return false;
        }
        return true;
    }

    saveRecommedTime() {
        Tools.setStorage("recommendTime", new Date().getTime());
    }

    canShowAddShortcutUI() {
        let time = Tools.getStorage("shortcut");
        if (!time) return true;
        let timeNow = new Date();
        let timeLast = new Date(time);
        cc.log(timeNow.getMonth(), timeLast.getMonth(), timeNow.getDay(), timeLast.getDay())
        if (timeNow.getMonth() === timeLast.getMonth() && timeNow.getDay() === timeLast.getDay()) {
            /** 同一天 */
            return false;
        }
        return true;
    }

    saveAddShortcutTime() {
        Tools.setStorage("shortcut", new Date().getTime());
    }

    canShowSubscribe() {
        let isSub = Tools.getStorage("subscribe");
        if (isSub) return false;
        let time = Tools.getStorage("subscribeTime");
        if (!time) return true;
        let timeNow = new Date();
        let timeLast = new Date(time);
        cc.log(timeNow.getMonth(), timeLast.getMonth(), timeNow.getDay(), timeLast.getDay())
        if (timeNow.getMonth() === timeLast.getMonth() && timeNow.getDay() === timeLast.getDay()) {
            /** 同一天 */
            return false;
        }
        return true;
    }

    saveShowSubscribe() {
        Tools.setStorage("subscribeTime", new Date().getTime());
    }

    resetLiveLevelBtn() {
        Tools.setStorage("setLiveLvActive", false);
    }

}